// Appendix8.1
log using Appendix8.1.log, replace

use "C:\Users\sbstjp\OneDrive - Cardiff University\anes_timeseries_2020_stata_20220210.dta" // American National Election Study 2020 Time Series Study, Feb 10, 2022 Version. Date accessed: March 09, 2025. Accessed from https://electionstudies.org/data-center/2020-time-series-study/

// Social justice scale 
*Delete missing values
rename V201411x tgpolicy 
replace tgpolicy = . if tgpolicy == -2
rename V201626 offence 
replace offence = . if inlist(offence, -5, -9)
rename V202183 metoo 
replace metoo = . if inlist(metoo, -9, -7, -6, -5, -4, 998, 999)
rename V202174 blm 
replace blm = . if inlist(blm, -9, -7, -6, -5, -4, 998, 999)

*Reverse code offence so social justice values are high
egen maxval = max(offence)
gen roffence = maxval + 1 - offence
drop maxval

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in tgpolicy roffence metoo blm {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in stgpolicy sroffence smetoo sblm  {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in stgpolicy sroffence smetoo sblm  {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Demographic variables
* Delete missing values
rename V201507x age 
replace age = . if age == -9

rename V201617x income 
replace income = . if inlist(income, -9, -5)

rename V201600 FemaleGender 
replace FemaleGender = . if FemaleGender == -9

rename V201510 education 
replace education = . if inlist(education, -9, -8, 95)

rename V201549x race
replace race = . if inlist(race, -9, -8)

rename V201200 libconsp 
replace libconsp = . if inlist(libconsp, -9, -8, 99)

*Create dummy variables
gen Graduate=.
replace Graduate=0 if education<6
replace Graduate=1 if inrange(education, 6, 8)

gen BIPOC=. 
replace BIPOC=0 if race==1
replace BIPOC=1 if inrange(race, 2, 6)

// Liberalism scale
*Delete missing values
rename V201415 gayadopt
replace gayadopt = . if inlist(gayadopt, -8, -9)
rename V201336 abortion
replace abortion = . if inlist(abortion, -8, -9, 5) 
rename V201262 environment
replace environment = . if inlist(environment, -9, -8, 99)
rename V202232 immigration
replace immigration = . if inlist(immigration, -9, -8, -7, -6, -5) 
rename V201345x deathpen
replace deathpen = . if inlist(deathpen, -2) 
rename V201426x wall
replace wall = . if inlist(wall, -2) 
rename V201308x bordersec
replace bordersec = . if inlist(bordersec, -2) 
rename V201311x crime
replace crime = . if inlist(crime, -2) 

*Reverse variables so liberalism coded high
foreach var in gayadopt immigration environment {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

*Standardize items in the scale from 1-2 - this avoids 0, for reasons outlined in next step
foreach var in rgayadopt abortion renvironment rimmigration deathpen wall bordersec crime {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in srgayadopt sabortion srenvironment srimmigration sdeathpen swall sbordersec scrime {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreAL = 0
gen count_nonzeroAL = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in srgayadopt sabortion srenvironment srimmigration sdeathpen swall sbordersec scrime {
    replace total_scoreAL = total_scoreAL + `var'
    replace count_nonzeroAL = count_nonzeroAL + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen LibValues = .
replace LibValues = total_scoreAL / count_nonzeroAL if count_nonzeroAL > 0

// Democracy variables

*Delete missing values
replace V201366 = . if inlist(V201366, -9, -8) 
replace V201367 = . if inlist(V201367, -9, -8) 

*Standardize items in the scale from 1-2, so coefficients can be compared to others in book
foreach var in V201366 V201367 {
    summarize `var'
    gen s`var' = 1 + (`var' - r(min)) / (r(max) - r(min))
}

*Rename
rename sV201366 NewsOrgs
rename sV201367 BranchesOfGov

// Standardize variables 
egen Age = std(age)
egen Income = std(income)

// Regressions
regress NewsOrgs LibValues [pweight=V200010b], robust 
eststo
regress NewsOrgs LibValues Age FemaleGender Graduate Income BIPOC [pweight=V200010b], robust 
eststo
regress NewsOrgs SocJusValues [pweight=V200010b], robust 
eststo
regress NewsOrgs SocJusValues Age FemaleGender Graduate Income BIPOC [pweight=V200010b], robust  
eststo
regress NewsOrgs LibValues SocJusValues Age FemaleGender Graduate Income BIPOC [pweight=V200010b], robust 
eststo
esttab

eststo clear

regress BranchesOfGov LibValues [pweight=V200010b], robust 
eststo
regress BranchesOfGov LibValues Age FemaleGender Graduate Income BIPOC [pweight=V200010b], robust 
eststo
regress BranchesOfGov SocJusValues [pweight=V200010b], robust 
eststo
regress BranchesOfGov SocJusValues Age FemaleGender Graduate Income BIPOC [pweight=V200010b], robust 
eststo
regress BranchesOfGov LibValues SocJusValues Age FemaleGender Graduate Income BIPOC [pweight=V200010b], robust 
eststo
esttab

log close

eststo clear

// Alternative specification
ologit NewsOrgs SocJusValues Age FemaleGender Graduate Income BIPOC [pweight=V200010b], robust // Alternative specification

// Coefficient plot
regress BranchesOfGov LibValues SocJusValues Age FemaleGender Graduate Income BIPOC [pweight=V200010b], robust
label variable Age ""
label variable FemaleGender ""
label variable Income ""
coefplot, drop(_cons) xline(0, lcolor(red) lwidth(medium)) scheme(white_jet) xtitle("{bf: Support for separation of powers}")

